<?php
/**
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file license.txt or that can be found at 
 * http://code.google.com/p/billboardce/wiki/License.
 *
 * @package    Transition_Module
 * @category   Model
 * @license    http://code.google.com/p/billboardce/wiki/License New BSD License
 * @version    SVN: $Id: $
 */
class Transition_Module implements Plugin_Slide_Option_Interface
{
    public function getTitle()
    {
        return 'Transition';
    }
    
    public function getOptionForm($slideId)
    {
        $transition = new Transition_Data();
        
        $data = $transition->find($slideId);
        if (is_null($data)) {
            $data = array(
                'slideId' => $slideId,
            );
        } else {
            $data = $data->toArray();
        }
        
        return $transition->form($data)->__toString();
    }
    
    public function getData($slideId)
    {
        
        $transition = new Transition_Data();
        
        $data = $transition->find($slideId);
        if (is_null($data)) {
            return array();
        } else {
            
            $retArray = array();
            
            $retArray['transition'] = $data->transition;
            
            return $retArray;
        }
    }
    
    public function saveOptionForm($slideId, array $data)
    {
        
        $transition = new Transition_Data();
        
        $form = $transition->form(array('slideId' => $slideId));
        
        if ($form->isValid($data)) {
            $existingData = $transition->find($slideId);

            $data['slideId'] = $slideId;
            
            if (is_null($existingData)) {
                $transition->insert($data);
            } else {
                $transition->update($data, null);
            }
        }
        
        return $form->getErrorMessages();
    }
}